<?php declare(strict_types=1);

namespace App\Services\Vbill;

use App\Api\Controllers\Config\VbillConfigController;
use App\Exceptions\BizException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class IncomeResultService
{

    public function query(string $store_id): array
    {
        $info = DB::table("vbill_sub_infos")->where([
            'store_id' => $store_id,
        ])->first();
        if (!empty($info) && !empty($info->weixin_mch_id)  && !empty($info->alipay_mch_id)) {
            return [true, $info];
        }

        $vbillStore = Db::table('vbill_stores')->where(['store_id' => $store_id])->first();
        if (empty($vbillStore)) {
            return [false, "商户号不存在"];
        }
        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($vbillStore->config_id);
        $obj = new \App\Api\Controllers\Vbill\BaseController();
        $url = $obj->weixin_config;

        $url = $obj->open_query_store_url;
        $data = [
            'orgId' => $vbill_config->orgId,
            'reqId' => time(),
            'version' => '1.0',
            'timestamp' => date('Ymdhis', time()),
            'signType' => 'RSA',
            'reqData' => [
                'applicationId' => $vbillStore->applicationId, // f76c284da5644575aabe94043ccb637d
            ],
        ];
        $res = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
        Log::channel("auth")->info("IncomeResultService-query", [
            "url" => $url,
            "req" => $data,
            "res" => $res
        ]);
        if (!isset($res['data']['respData']['taskStatus'])) {
            $msg = $res['data']['respData']["bizMsg"];
            Db::table('store_pay_ways')->where(['store_id' => $store_id, "company" => "vbill"])
                ->update(['status' => 3, 'status_desc' => '开通失败:' . $msg]);
            return [false, "入审核驻失败_" . $msg];
        }
        if ($res['data']['respData']['taskStatus'] != "1") {
            if (isset($res['data']['respData']['suggestion']) && !empty($res['data']['respData']['suggestion'])) {
                $msg = $res['data']['respData']['suggestion'];
            }
            Db::table('store_pay_ways')->where(['store_id' => $store_id, "company" => "vbill"])
                ->update(['status' => 3, 'status_desc' => '开通失败:' . $msg]);
            return [false, "入审核驻失败_" . $msg];
        }
        Db::table('store_pay_ways')->where(['store_id' => $store_id, "company" => "vbill"])->update(['status' => 1, 'status_desc' => '开通成功']);
        $mno = $res['data']['respData']['mno'];
        $repoInfo = $res['data']['respData']['repoInfo'] ?? [];
        $weixin_mch_id = $alipay_mch_id = $yl_mch_id = "";
        if (!empty($repoInfo)) {
            foreach ($repoInfo as $k => $v) {
                if ($v["childNoType"] == "YL-MNO") {
                    $yl_mch_id = $v["childNo"];
                }

                if ($v["childNoType"] == "ZFB") {
                    $alipay_mch_id = $v["childNo"];
                }

                if ($v["childNoType"] == "WX") {
                    $weixin_mch_id = $v["childNo"];
                }
            }
        }
        //写入db
        if (empty($info)) {
            DB::table("vbill_sub_infos")->insert([
                "mno" => $mno,
                "store_id" => $store_id,
                "weixin_mch_id" => $weixin_mch_id,
                "alipay_mch_id" => $alipay_mch_id,
                "yl_mch_id" => $yl_mch_id,
                "created_at" => date("Y-m-d H:i:s", time()),
                "updated_at" => date("Y-m-d H:i:s", time())
            ]);
        } else {
            DB::table("vbill_sub_infos")->where("id", $info->id)->update([
                "mno" => $mno,
                "store_id" => $store_id,
                "weixin_mch_id" => $weixin_mch_id,
                "alipay_mch_id" => $alipay_mch_id,
                "yl_mch_id" => $yl_mch_id,
                "created_at" => date("Y-m-d H:i:s", time()),
                "updated_at" => date("Y-m-d H:i:s", time())
            ]);
        }
        $info = DB::table("vbill_sub_infos")->where([
            'store_id' => $store_id,
        ])->first();
        return [true, $info];
    }

}